package top.zhangjianyong.netty.nio;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
import java.util.Scanner;

public class NIOClient {
    public static void main(String[] args) throws IOException {
        SocketChannel socketChannel = SocketChannel.open();

        socketChannel.configureBlocking(false);
        InetSocketAddress inetSocketAddress = new InetSocketAddress("127.0.0.1", 6666);

        if (!socketChannel.connect(inetSocketAddress)) {
            while (!socketChannel.finishConnect()) {
                System.out.println("another thing ....");
            }
        }

        String msg ="hello,abc";
        ByteBuffer buffer = ByteBuffer.wrap(msg.getBytes());
        socketChannel.write(buffer);
        Scanner scanner =new Scanner(System.in);
        while (scanner.hasNext()){
            String next = scanner.next();
            ByteBuffer wrap = ByteBuffer.wrap(next.getBytes());
            socketChannel.write(wrap);
        }
    }
}
